home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 021 (1987-08-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 021 (1987-08-15)(Ossowski, Stefan)(DE)(PD).adf / source / Display.c
C/C++ Source or Header  |  1986-10-24  |  7KB  |  271 lines

  1. /*
  2. From: wecker%cookie.DEC@decwrl.DEC.COM  (DAVE  TANSTAAFL  WECKER)
  3.  
  4. Doc:    Here is the source for a display program that can be used with the
  5.     image files that follow. I've been working with a Ray Tracing program,
  6.     and finally figured out how to take full advantage of the Amiga's
  7.     HAM (Hold and Modify) mode, yielding 4096 colors on the screen at
  8.     once.
  9.  
  10.     If people like the pictures, I'll send more (they take from 1 hour
  11.     to several days on a dedicated VAX).
  12.  
  13.     To use the program, compile and link it (nothing special needed) and
  14.     then type: 
  15.  
  16.         1> DISPLAY FOO.IMG 
  17.  
  18.     After the picture completes you can exit by clicking on the (invisible)
  19.     close gadget in the upper left corner. I know the program is (very)
  20.     rough, but I haven't had time to make it pretty.. just functional.
  21.  
  22.     Please post it and let me know what you think.
  23.  
  24.     Regards,
  25.     dave
  26.  
  27. --------------------Program begins below this line--------------------------*/
  28. /***********************************************************************
  29.  *
  30.  *  DISPLAY package for .img files (DBW encoding)
  31.  *
  32.  *    File format:
  33.  *        MAXX MAXY MAXCOLORS                    - header
  34.  *        256 hex colors in descending usage      - colortable
  35.  *        Encoding of pixels: (all printable ASCII)
  36.  *        @ - O    = Absolute color (low four bits) in color table
  37.  *        P - _    = Relative color. Low four bits is new BLUE  value
  38.  *        ` - o    = Relative color. Low four bits is new RED   value
  39.  *        p - DEL    = Relative color. Low four bits is new GREEN value
  40.  *        ' ' - >    = Repeat introducer (next char - ' ' = repeat count)
  41.  *        ?       = Alternate encoding for <DEL>
  42.  *        \r \n    = Ignored (end of line markers) (NOT scan line)
  43.  *
  44.  *    v1.0    860907    DBW
  45.  *
  46.  ***********************************************************************/
  47.  
  48. #include <stdio.h>
  49. #include <exec/types.h>
  50. #include <intuition/intuition.h>
  51. #include <intuition/intuitionbase.h>
  52. #include <functions.h>
  53.  
  54. extern    char    *fgets();
  55.  
  56. #undef    NULL
  57. #define    NULL    ((void *)0)
  58.  
  59. #define CHECK(x)    while (line[x] < ' ') {\
  60.     if (fgets(line,128,fp) == NULL) return(-1);\
  61.     x = 0;\
  62.     }
  63.  
  64. struct    GfxBase        *GfxBase;
  65. struct    IntuitionBase    *IntuitionBase;
  66. struct    RastPort    *rp;
  67. struct    ViewPort    *vp;
  68. struct    Window        *w;
  69. struct    Screen        *screen;
  70. struct    IntuiMessage    *message;
  71. struct    NewScreen    ns = {
  72.     0L,0L,320L,400L,6L,
  73.     0,1,HAM|LACE,
  74.     CUSTOMSCREEN,NULL,
  75.     (UBYTE *)"Display screen",
  76.     NULL,NULL };
  77. struct    NewWindow    nw = {
  78.     0L,0L,320L,400L,0L,1L,
  79.     MOUSEBUTTONS|CLOSEWINDOW,
  80.     ACTIVATE|WINDOWCLOSE|BORDERLESS,
  81.     NULL,NULL,
  82.     (UBYTE *)"Display window",
  83.     NULL,NULL,
  84.     0L,0L,320L,400L,CUSTOMSCREEN };
  85.  
  86. FILE        *fp = NULL;
  87. unsigned char    line[128];
  88. int        maxx,maxy,maxc;
  89. int        colors[16];
  90. int        cpix,cr,cg,cb,count,pos;
  91.  
  92. main(argc,argv)
  93. int argc;
  94. char *argv[];
  95.     {
  96.     unsigned long class;
  97.     unsigned short code;
  98.     int        i,x,y,val,bflg;
  99.  
  100.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L);
  101.     if (GfxBase == NULL) exit(100);
  102.  
  103.     IntuitionBase = 
  104.     (struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  105.     if (IntuitionBase == NULL) {
  106.     CloseLibrary(GfxBase);
  107.     exit(200);
  108.     }
  109.  
  110.     screen = (struct Screen *)OpenScreen(&ns);
  111.     if (screen == NULL) {
  112.     CloseLibrary(IntuitionBase);
  113.     CloseLibrary(GfxBase);
  114.     exit(300);
  115.     }
  116.  
  117.     nw.Screen = screen;
  118.     w          = (struct Window *)OpenWindow(&nw);
  119.     if (w == NULL) {
  120.     CloseScreen(screen);
  121.     CloseLibrary(IntuitionBase);
  122.     CloseLibrary(GfxBase);
  123.     exit(400);
  124.     }
  125.  
  126.     vp = &screen->ViewPort;
  127.     rp = w->RPort;
  128.  
  129.     /* get the file to read */
  130.     if (argc < 2) fp = stdin;
  131.     else      fp = fopen(argv[1],"r");
  132.  
  133.     if (fp == NULL) error("Can't open input file");
  134.     if (fgets(line,128,fp) == NULL) error("Can't read first line");
  135.     if (sscanf(line,"%d %d %d\n",&maxx,&maxy,&maxc) != 3) error("Bad header");
  136.     if (maxc != 256) error("expecting 256 colors");
  137.     for (i = 0; i < 16; i++) {
  138.     if (fgets(line,128,fp) == NULL) error("Can't read colors");
  139.     if (i == 0 &&
  140.         sscanf(line,"%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
  141.          &colors[0],&colors[1],&colors[2],&colors[3],&colors[4],&colors[5],
  142.          &colors[6],&colors[7],&colors[8],&colors[9],&colors[10],&colors[11],
  143.          &colors[12],&colors[13],&colors[14],&colors[15]) != 16)
  144.         error("Didn't find 16 colors as expected");
  145.     }
  146.  
  147.     /* now set the color map */
  148.     for (i = 0; i < 16; i++) {
  149.     SetRGB4(vp, (long)i, (long)((colors[i] >> 8) & 0xF),
  150.                  (long)((colors[i] >> 4) & 0xF),
  151.                  (long)((colors[i]     ) & 0xF));
  152.     }
  153.  
  154.     /* now init the read buffer */
  155.     line[0] = '\n';
  156.     pos        = 0;
  157.     count   = 0;
  158.     cr        = 0;
  159.     cg        = 0;
  160.     cb        = 0;
  161.     cpix    = '@';
  162.     SetDrMd(rp,JAM1);
  163.  
  164.     /* Read through the file and display the results */
  165.     bflg = 0;
  166.     for (y = 0; y < maxy; y++) {
  167.  
  168.     /* check if there was an attempt to abort */
  169.     while ((message=(struct IntuiMessage *)GetMsg(w->UserPort))!=NULL) {
  170.         class   = message->Class;
  171.         code    = message->Code;
  172.         ReplyMsg(message);
  173.  
  174.         if (class == CLOSEWINDOW) error(NULL);
  175.         }
  176.  
  177.     /* now dump the scan line */
  178.     for (x = 0; x < maxx; x++) {
  179.         if (getnext() == -1) {
  180.         bflg = 1;
  181.         break;
  182.         }
  183. /*
  184.         val  = ((cr * 7 + cg * 4 + cb * 2) / 2) + 32;
  185.         if (val > 126) val = 126;
  186.         if ((y % 10) == 9 && (x % 4) == 3) printf("%c",(char)val);
  187. */        
  188.         if (x < 320 && y < 400) {
  189.         SetAPen(rp,(long)(cpix & 0x3F));
  190.         WritePixel(rp,(long)x,(long)y);
  191.         }
  192.         }
  193.     if (bflg == 1) break;
  194. /*
  195.     if ((y % 10) == 9) printf("\n");
  196. */
  197.     }
  198.  
  199.     /* wait here until we get a close window message */
  200.     while (1) {
  201.     WaitPort(w->UserPort);
  202.     while ((message=(struct IntuiMessage *)GetMsg(w->UserPort))!=NULL) {
  203.         class   = message->Class;
  204.         code    = message->Code;
  205.         ReplyMsg(message);
  206.  
  207.         if (class == CLOSEWINDOW) error(NULL);
  208.         }
  209.     }
  210.     }
  211.  
  212. error(msg)
  213. char *msg;
  214.     {
  215.     if (msg) {
  216.     puts("ERROR: ");
  217.     puts(msg);
  218.     puts("\n");
  219.     }
  220.     CloseWindow(w);
  221.     CloseScreen(screen);
  222.     CloseLibrary(IntuitionBase);
  223.     CloseLibrary(GfxBase);
  224.     if (fp != NULL && fp != stdin) fclose(fp);
  225.     if (msg) exit(-1);
  226.     else     exit(0);
  227.     }
  228.  
  229. getnext() {
  230.     int    scr,crgb;
  231.  
  232.     /* first see if we're in a repeat count */
  233.     if (count-- > 0) return(0);
  234.  
  235.     /* now see if we need a new buffer */
  236.     CHECK(pos);
  237.  
  238.     /* see if we got a repeat count */
  239.     if (line[pos] < '?') {
  240.     count = (line[pos] - ' ') * 94;
  241.     ++pos;
  242.     CHECK(pos);
  243.     count += line[pos++] - ' ';
  244.     CHECK(pos);
  245.     }
  246.     else
  247.     count = 1;
  248.  
  249.     /* now bring the character in */
  250.     cpix = line[pos++];
  251.     if (cpix == '?') cpix = 127;
  252.     if (--count < 0) return(0);
  253.  
  254.     /* now set up all the associated variables */
  255.     scr = (cpix >> 4) & 3;
  256.     switch (scr) {
  257.     case 0:    crgb = colors[cpix & 0xF];
  258.         cr   = (crgb >> 8) & 0xF;
  259.         cg   = (crgb >> 4) & 0xF;
  260.         cb   = (crgb     ) & 0xF;
  261.         break;
  262.     case 1:    cb   = cpix & 0xF;
  263.         break;
  264.     case 2:    cr   = cpix & 0xF;
  265.         break;
  266.     case 3:    cg   = cpix & 0xF;
  267.         break;
  268.     }
  269.     return(0);
  270.     }
  271.